'''初始化'''
# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
from django.conf import settings

def create_bucket(bucket,region='ap-nanjing'):
    '''创建桶'''

    cos_secret_id = 'AKID3xd5MRIuKi292g5FApFRO5XjgPt88vUo'  # 替换为用户的 SecretId，请登录访问管理控制台进行查看和管理，https://console.cloud.tencent.com/cam/capi
    cos_secret_key = 'FH5ex3vYw4xbz82g4D6nHVgleJm5D9rN'
    config = CosConfig(Region=region, SecretId=cos_secret_id, SecretKey=cos_secret_key)
    client = CosS3Client(config)
    '''创建桶'''
    response = client.create_bucket(
        Bucket=bucket,
        ACL="public-read"
    )

def upload_file(bucket,image_object,key,region='ap-nanjing'):
    cos_secret_id = 'AKID3xd5MRIuKi292g5FApFRO5XjgPt88vUo'  # 替换为用户的 SecretId，请登录访问管理控制台进行查看和管理，https://console.cloud.tencent.com/cam/capi
    cos_secret_key = 'FH5ex3vYw4xbz82g4D6nHVgleJm5D9rN'
    config = CosConfig(Region=region, SecretId=cos_secret_id, SecretKey=cos_secret_key)
    client = CosS3Client(config)
    response = client.upload_file_from_buffer(
        Bucket=bucket,
        Body=image_object, # 上传的文件对象
        Key=key,  # 上传到桶之后的文件名
    )
    # 返回文件路径

    return "https://{}.cos.{}.myqcloud.com/{}".format(bucket, region, key)

